import Vue from 'vue'
import Router from 'vue-router'
import list from '@/components/list'
Vue.use(Router)

// eslint-disable-next-line no-unused-vars
let router;
// eslint-disable-next-line no-unused-vars
export default router = new Router({
    routes: [
        {
            path: '/list',
            name: 'list',
            component: list
        },
        {
            path: '/',
            name: 'home',
            component: () => import('@/views/Home.vue'),
            children:[
                {
                    path: '/trademark',
                    name: 'trademark',
                    component: () => import('@/views/product/Trademark.1.vue')
                },
                {
                    path: '/categoryDTO',
                    name: 'categoryDTO',
                    component: () => import('@/views/product/CategoryDTO.1.vue')
                },
                {
                    path: '/categoryTrademark',
                    name: 'categoryTrademark',
                    component: () => import('@/views/product/CategoryTrademark.vue')
                },
                {
                    path: '/attr',
                    name: 'attr',
                    component: () => import('@/views/product/Attr.vue')
                },
                {
                    path: '/spu',
                    name: 'spu',
                    component: () => import('@/views/product/Spu.vue')
                },
                {
                    path: '/sku',
                    name: 'sku',
                    component: () => import('@/views/product/Sku.vue')
                },
                {
                    path: '/searchSku',
                    name: 'searchSku',
                    component: () => import('@/views/search/SearchSKU.vue')
                },
                {
                    path: '/livePush',
                    name: 'livePush',
                    component: () => import('@/views/live/livePush.vue')
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/login.vue')
        },
        
    ]
})

router.beforeEach((to, from, next) => {
    //to 打算到哪里
    //from 从哪来
    /* 必须调用 `next` */
    console.log(from,to)
    if(to.path === '/login' || to.path === '/reg'){
        sessionStorage.removeItem("userId");
        sessionStorage.removeItem("token");
        next();//该去哪去哪
    }else if(to.path === '/logout'){
        sessionStorage.removeItem("userId");
        sessionStorage.removeItem("token");
        next({path: '/login'})
    }else{
        let token = sessionStorage.getItem("token");
        if(token == null){
            next({path: '/login'})
        }else{
            next();
        }
    }
})
  